﻿using Microsoft.VisualBasic.ApplicationServices;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Utilities.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace SmartRFIDSystem.dao
{
    public class UserDao
    {
        //语句执行对象
        private MySqlCommand comm = null;
        private MySqlConnection conn = null;
        private string M_str_sqlcon = "server=localhost;user id=root;password=3027689312a;database=contactssystem";
        public int register(string userName, string passWord, string power)
        {
            //数据库连接
            try
            {
                conn = new MySqlConnection(M_str_sqlcon);
                conn.Open();
               // MessageBox.Show("成功");

            }
            catch
            {

            }

            string sqlStr = "insert into user (id,name,password,power) values (@id,@name,@password,@power)";
            comm = new MySqlCommand(sqlStr, conn);
            comm.Parameters.Add(new MySqlParameter("@id", null));
            comm.Parameters.Add(new MySqlParameter("@name", userName));
            comm.Parameters.Add(new MySqlParameter("@password", passWord));
            comm.Parameters.Add(new MySqlParameter("@power", power));
            try
            {
                int r = comm.ExecuteNonQuery();
                if (r == 1)
                {
                    MessageBox.Show("注册成功");
                    return 1;
                }
                else
                {
                    MessageBox.Show("注册失败");
                    return -1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("注册失败");
                return -1;
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }

            //关闭连接
            return 0;   
        }
        public int login(string userName, string passWord, string power)
        {
            //数据库连接
            try
            {
                conn = new MySqlConnection(M_str_sqlcon);
                conn.Open();
                // MessageBox.Show("成功");

            }
            catch
            {

            }

            string sqlStr = "select*from user where name=@name and password=@password and power=@power";
            comm = new MySqlCommand(sqlStr, conn);
            comm.Parameters.Add(new MySqlParameter("@name", userName));
            comm.Parameters.Add(new MySqlParameter("@password", passWord));
            comm.Parameters.Add(new MySqlParameter("@power", power));
            try
            {
                object id = comm.ExecuteScalar();
                if (id == null)
                {
                    MessageBox.Show("用户名密码错误！");
                    return -1;
                }
                else
                {
                    MessageBox.Show("登录成功！");
                    return 1;
                }
            }
            catch
            {
                MessageBox.Show("用户名密码错误！");
                return -1;
            }

            finally
            {
                if (conn != null)
                    conn.Close();
            }

            //关闭连接

            return 0;   
        }
        public void ChangePassword(string userName, string passWord, string power)
        {
            //数据库连接
            try
            {
                conn = new MySqlConnection(M_str_sqlcon);
                conn.Open();
                // MessageBox.Show("成功");

            }
            catch
            {

            }

            string sqlStr = "update user set password=@password where name=@name and power=@power";
            comm = new MySqlCommand(sqlStr, conn);
            comm.Parameters.Add(new MySqlParameter("@name", userName));
            comm.Parameters.Add(new MySqlParameter("@password", passWord));
            comm.Parameters.Add(new MySqlParameter("@power", power));
            try
            {
                int r = comm.ExecuteNonQuery();
                if (r == 1)
                {
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败");
            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }

            //关闭连接
        }

    }
}
